package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.uqbike.object.pojo.UniAppMenu;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.UniAppMenuService;
import com.tbit.uqbike.webmanager.dao.core.UniAppMenuDao;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author:BUGTian
 * @DATE: 2022/9/20
 */
@Service
public class UniAppMenuServiceImpl extends ServiceImpl<UniAppMenuDao, UniAppMenu> implements UniAppMenuService {

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public Result edit(List<UniAppMenu> uniAppMenus) {
        //校验数据
        UniAppMenu uniAppMenu = uniAppMenus.get(0);
        Integer accountId = uniAppMenu.getAccountId();
        Integer type = uniAppMenu.getType();
        int i = 1;
        for (UniAppMenu appMenu : uniAppMenus) {
            if (!Objects.equals(appMenu.getAccountId(), accountId) || !Objects.equals(appMenu.getType(), type)) {
                return Result.error("参数有误");
            }
            appMenu.setSort(i++);
        }
        remove(new QueryWrapper<UniAppMenu>().eq("accountId", accountId).eq("type", type));
        saveBatch(uniAppMenus);
        return Result.success();
    }

    @Override
    public Result<BatchOperationMsgVO<String>> addMenuAndRearrange(UniAppMenu uniAppMenu) {
        //1.先捞出所有=2的配置
        QueryWrapper<UniAppMenu> queryWrapper = new QueryWrapper<UniAppMenu>().eq("type", uniAppMenu.getType());
        List<UniAppMenu> menuList = list(queryWrapper);
        if (menuList.isEmpty()) {
            return Result.error(String.format("无类型为%d的数据", uniAppMenu.getType()));
        }
        BatchOperationMsgVO<String> resultVo = new BatchOperationMsgVO<>();
        //2.过滤已经添加的运行区域
        Map<Integer, List<UniAppMenu>> accountMenuGroup = menuList.stream().collect(Collectors.groupingBy(UniAppMenu::getAccountId));
        for (Map.Entry<Integer, List<UniAppMenu>> entry : accountMenuGroup.entrySet()) {
            Integer accountId = entry.getKey();
            List<UniAppMenu> menus = entry.getValue();
            boolean existed = menus.stream().anyMatch(x -> Objects.equals(uniAppMenu.getName(), x.getName()));
            //区域已存在 不添加
            if (existed) {
                resultVo.putAbnormalValue(String.format("菜单已存在,区域id-%d", accountId));
                continue;
            }
            List<UniAppMenu> rearrangeList = menus.stream().filter(x -> x.getSort() >= uniAppMenu.getSort()).collect(Collectors.toList());
            //没有需要重排的 不添加
            if (rearrangeList.isEmpty()) {
                resultVo.putAbnormalValue(String.format("菜单列表最大值为%d无可重排的数据区域id-%d", menus.size(), accountId));
                continue;
            }
            rearrangeList.stream()
                    .sorted(Comparator.comparing(UniAppMenu::getSort).reversed())
                    .forEach(x -> {
                        LambdaUpdateWrapper<UniAppMenu> queryWrapper2 = new LambdaUpdateWrapper<>();
                        queryWrapper2.eq(UniAppMenu::getAccountId, x.getAccountId());
                        queryWrapper2.eq(UniAppMenu::getIcon, x.getIcon());
                        queryWrapper2.eq(UniAppMenu::getName, x.getName());
                        queryWrapper2.eq(UniAppMenu::getType, uniAppMenu.getType());
                        x.setSort(x.getSort() + 1);
                        update(x, queryWrapper2);
                    });
            uniAppMenu.setAccountId(accountId);
            save(uniAppMenu);
            resultVo.putSuccessValue(accountId.toString());
        }
        return Result.success(resultVo);
    }


}
